package com.ybxy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {

    public static void main(String[] args) {
        // test0();
        //test1();
        //test2();
        test3();
    }

    private static void test0() {
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("赵六");
        list.add("张七1");
        list.add("张八二");
        list.add("张九一");

        List<String> list1 = new ArrayList();
        list.forEach(name->{
            if (name.startsWith("张")){
                list1.add(name);
            }
        });

        System.out.println(list1);


        List<String> list2 = new ArrayList<>();

        list1.forEach(name->{
            if (name.length() == 3 ){
                list2.add(name);
            }
        });
        System.out.println(list2);

        System.out.println("--------------------stream------------------------");
        //1.获取集合的流对象，
        //filter过滤，返回流对象，forEach遍历
        list.stream()
                .filter(name->name.startsWith("张"))
                .filter(name->name.length() == 3)
                .forEach(System.out::println);
    }


    public static void  test1(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);

        list.stream().filter(integer -> integer % 2 == 0).forEach(System.out::println);

        //流的收集方法
        List<Integer> list1 = list.stream().filter(integer -> integer % 2 == 0).collect(Collectors.toList());

    }

    public static void test2(){
       List<String> list = new ArrayList<>();
       list.add("张三,23");
       list.add("李四,24");
       list.add("王五,25");
       list.add("赵六,26");
       list.add("张七,27");
       list.add("张八,28");
        Map<String,String> map = list.stream().filter(s->{
            String[] split = s.split(",");
           int age = Integer.parseInt(split[1]);
           return age > 25;
        }).collect(Collectors.toMap(s->{
            String[] split = s.split(",");
            return split[0];
        },s->{
            String[] split = s.split(",");
            return split[1];
        }));

        map.forEach((k,v)->{
            System.out.println(k+"---"+v);
        });
       //过滤年龄大于25

    }

    public static  void test3(){
        List<String> list1 = new ArrayList<>();
        //男演员
        list1.add("吴彦祖");
        list1.add("彭于晏");
        list1.add("陈冠希");
        list1.add("李易峰");
        list1.add("张学友");

        List<String> list2 = new ArrayList<>();
        //女演员
        list2.add("林志玲");
        list2.add("赵丽颖");
        list2.add("杨幂");
        list2.add("杨颖");
        list2.add("古力娜扎");

        Stream<String> stream1 =list1.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length() == 3;
            }
        }).limit(2);

        Stream<String> stream2 = list2.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("杨");
            }
        }).skip(1);
        Stream.concat(stream1,stream2).forEach(System.out::println);

    }

}
